﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqData;
using FoneBuddy.LinqModel.Data;
using FoneBuddy.LinqModel;

public partial class MerchantSubscription : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();
    Merchant merchant = new Merchant();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            merchant = SessionWrapper.Merchant;
            BindSubscription();

           
        }
       
    }

    #region Bind Subscription
    private void BindSubscription()
    {
        Subscription subscription = new Subscription();
        subscription = new Subscription(_dataObject);
        List<Subscription> subscriptionList = subscription.GetAllSubscription();
        //if (merchant.Id > 0)
        //    subscriptionList = subscriptionList.Where(w => w.Amount != 0).ToList();
        rptSubscription.DataSource = subscriptionList;
        rptSubscription.DataBind();
       

    }
    #endregion

    #region Save Merchant,Address  and SubscriptionInfo
    protected void OnSelectSubscription(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        RepeaterItem Item = btn.NamingContainer as RepeaterItem;
        Int32 Index = Item.ItemIndex;
        Int32 SubscriptionId = Convert.ToInt32(btn.CommandArgument);
        Label lblAmount = rptSubscription.Items[Index].FindControl("lblAmount") as Label;

        //enter data in merchant's table
        merchant = new Merchant(_dataObject);
        merchant = SessionWrapper.Merchant;

     
        if (merchant != null)
            merchant.SubscriptionHistories.Clear();

        //get subscription Info by id
        Subscription subscription = new Subscription(_dataObject);
        subscription.Id = SubscriptionId;
        subscription.GetSubscriptionById();
        //enter data in merchant subscription
        SubscriptionHistory subscriptionHistory = new SubscriptionHistory(_dataObject);

        subscriptionHistory.SubscriptionId = SubscriptionId;
        subscriptionHistory.StartDate = DateTime.Now;
        subscriptionHistory.ExpiryDate = DateTime.Now.AddMonths(subscription.Duration);
        subscriptionHistory.IsDeleted = false;
        subscriptionHistory.CreatedDate = DateTime.Now;
        subscriptionHistory.IpAdded = HttpContext.Current.Request.UserHostAddress;
        subscriptionHistory.Discount = 0;
        subscriptionHistory.IsActive = true;
        subscriptionHistory.TotalAmount=subscription.Amount;
        subscriptionHistory.Discount=0;
      
        //if user press back button then previous object will be clear
        //assign object in sessoin
        subscriptionHistory.MerchantId = merchant.Id;

        //for free user
        merchant.SubscriptionHistories.Add(subscriptionHistory);
        if (lblAmount.Text == "0")
        {
            SaveFreeSubscriptionUser();
        }
       
        SessionWrapper.Merchant = merchant;
        Response.Redirect("MerchantPromotionCode.aspx");
    }
    private Int32 GetMonth(Int32 SubscriptionId)
    {
        Subscription subscription = new Subscription(_dataObject);
        subscription.Id = SubscriptionId;
        subscription.GetSubscriptionById();
        return subscription.Duration;
    }
    private void SaveFreeSubscriptionUser()
    {
        Merchant merchant = new Merchant(_dataObject);
        merchant = SessionWrapper.Merchant;
        merchant.SubscriptionHistories.ElementAt(0).IsSuccess = true;
        merchant.SubscriptionHistories.ElementAt(0).MerchantId = merchant.Id;
        merchant.Create();
        Response.Redirect("~/transactioncompleted.aspx");
    }
    #endregion

    protected void OnDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            
            System.Web.UI.HtmlControls.HtmlControl h1 = e.Item.FindControl("planNameHeading") as System.Web.UI.HtmlControls.HtmlControl;
            System.Web.UI.HtmlControls.HtmlControl bg = e.Item.FindControl("detailBackGround") as System.Web.UI.HtmlControls.HtmlControl;
            Label lblAmount = e.Item.FindControl("lblAmount") as Label;
            Button btnSave = e.Item.FindControl("btnSave") as Button;
            if (lblAmount.Text=="0")
            {
                if (merchant.Id > 0)
                {
                    SubscriptionHistory sh = new SubscriptionHistory(_dataObject);
                    sh.MerchantId = merchant.Id;
                    List<SubscriptionHistory> shList = sh.GetSubscriptionHistoryByMerchantId();
                    shList = shList.Where(w=>w.IsSuccess==true).ToList();
                    if(shList.Count>0)
                    btnSave.Enabled = false;
                }
            }

            h1.Attributes.Add("class", "plannameHeading");

            if (e.Item.ItemIndex == 0 || e.Item.ItemIndex == 0 + 3 || e.Item.ItemIndex == 0 + 6)
            {
                h1.Attributes.CssStyle.Add("background", "url(./img/green_title_bg.png)");
                bg.Attributes.CssStyle.Add("background", "url(./img/green_content_bg.png)");
                btnSave.Style.Add("background", "url(./img/green_btn_bg.png)");
            }
            if (e.Item.ItemIndex == 1 || e.Item.ItemIndex == 1 + 3 || e.Item.ItemIndex == 1 + 6)
            {
                h1.Attributes.CssStyle.Add("background", "url(./img/Orange_title_bg.png)");
                bg.Attributes.CssStyle.Add("background", "url(./img/orange_content_bg.png)");
                btnSave.Style.Add("background", "url(./img/orange_btn_bg.png)");
            }
            if (e.Item.ItemIndex == 2 || e.Item.ItemIndex == 2 + 3 || e.Item.ItemIndex == 2 + 6)
            {
                h1.Attributes.CssStyle.Add("background", "url(./img/blue_title_bg.png)");
                bg.Attributes.CssStyle.Add("background", "url(./img/blue_content_bg.png)");
                btnSave.Style.Add("background", "url(./img/blue_btn_bg.png)");
            }

        }

    }
}